package com.maixun.lib_business_college.widget

import android.content.Context
import android.content.res.Resources
import android.graphics.Color
import android.graphics.Typeface
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.widget.LinearLayout
import android.widget.TextView
import com.google.android.material.imageview.ShapeableImageView
import com.google.android.material.shape.ShapeAppearanceModel
import com.maixun.lib_business_college.R

/**
 * @author：Lmoumou
 * @date：2020/11/4
 */
class HomeDoctorCardView : LinearLayout {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        gravity = Gravity.CENTER_HORIZONTAL
        orientation = VERTICAL

        initHeadView(context)
        initNameView(context)
        initPositionView(context)
        initLabelView(context)
    }

    private fun initHeadView(context: Context) {
        val headView = ShapeableImageView(context)
        headView.setStrokeColorResource(android.R.color.white)
        headView.strokeWidth = dp2px(1F).toFloat()
        headView.shapeAppearanceModel = ShapeAppearanceModel.builder()
            .setAllCornerSizes(ShapeAppearanceModel.PILL)
            .build()
        val padding = dp2px(1F)
        headView.setPadding(padding, padding, padding, padding)
        headView.layoutParams = LayoutParams(dp2px(52F), dp2px(52F)).apply {
            this.setMargins(0, dp2px(15F), 0, 0)
        }
        headView.setImageResource(R.mipmap.image_default)

        headView.setImageResource(R.mipmap.image_default)

        addView(headView)
    }

    private fun initNameView(context: Context) {
        val nameTextView = TextView(context)
        nameTextView.setTextColor(Color.parseColor("#FFFFFF"))
        nameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15F)
        nameTextView.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        nameTextView.layoutParams =
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
                this.setMargins(0, dp2px(6F), 0, 0)
            }

        nameTextView.text = "李胜利"

        addView(nameTextView)
    }

    private fun initPositionView(context: Context) {
        val positionTextView = TextView(context)
        positionTextView.setTextColor(Color.parseColor("#FFFFFF"))
        positionTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12F)
        positionTextView.layoutParams =
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)

        positionTextView.text = "主任医生"

        addView(positionTextView)
    }

    private fun initLabelView(context: Context) {
        val labelTextView = TextView(context)
        labelTextView.setBackgroundResource(R.drawable.college_home_doc_label_bg)
        labelTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13F)
        labelTextView.setTextColor(Color.parseColor("#333333"))
        labelTextView.setPadding(dp2px(11F), dp2px(3F), dp2px(11F), dp2px(3F))
        labelTextView.layoutParams =
            LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT).apply {
                this.setMargins(0, dp2px(3F), 0, dp2px(6F))
            }

        labelTextView.text = "产前超声"

        addView(labelTextView)
    }

    private fun dp2px(value: Float): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            value,
            Resources.getSystem().displayMetrics
        )
            .toInt()
    }
}